package com.app.common.ex

import java.text.SimpleDateFormat

/**
 *  把 3MIN 这类的文本转换成 秒
 * */
fun textTimeValueToSecValue(text:String?,default:Int):Int{
    if (text == null) {
        return default
    }
    return try {
        when  {
            text.endsWith("min",true) -> {
                text.substring(0,text.length - 3).trim().toInt() * 60
            }
            text.endsWith("sec",true) -> {
                text.substring(0,text.length - 3).trim().toInt()
            }
            else -> text.toInt()
        }
    }catch (e:Exception){
        default
    }
}

fun formatTime(se:Int):String{
    val h = se/(60*60)
    val m = se%(60*60)/60
    val s = se%60
    return String.format("%02d:%02d:%02d",  h,m, s)
}

fun formatTime2(se:Int):String{
    val h = se/(60*60)
    val m = se%(60*60)/60
    val s = se%60
    return if (h == 0) {
        String.format("%02d:%02d",  m, s)
    }else{
        String.format("%02d:%02d:%02d",  h,m, s)
    }
}
fun formatTime3(millisecond:Int):String{
    if(millisecond<=0)return "00:00"
    val se = millisecond / 1000
    val h = se/(60*60)
    val m = se%(60*60)/60
    val s = se%60
    return if (h == 0) {
        String.format("%02d:%02d",  m, s)
    }else{
        String.format("%02d:%02d:%02d",  h,m, s)
    }
}

fun Long.format(sample:String):String{
    return try {
        SimpleDateFormat(sample).format(this)
    }catch (e:Exception){
        ""
    }
}

fun String?.toTimestamp(pattern:String):Long{
    if (this.isNullOrEmpty()) {
        return 0
    }
    return try {
        SimpleDateFormat(pattern).parse(this.trim()).time
    }catch (e:Exception){
        e.printStackTrace()
        0
    }
}

fun to(list:List<*>):String{
    return list.joinToString ()
}